#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
class UF
{
public:
    UF(int N)
    {
        parents = new int[N + 1];
        fill(parents, parents + N + 1, -1);
        sizes = new int[N + 1];
        fill(sizes, sizes + N + 1, 0);
    }
    int Find(int x)
    {
        if (parents[x] == -1)
            return x;
        int par = Find(parents[x]);
        parents[x] = par;
        return par;
    }
    void Union(int x, int y)
    {
        int parx = Find(x), pary = Find(y);
        if (parx == pary)
            return;
        if (sizes[parx] < sizes[pary])
        {
            parents[parx] = pary;
            sizes[pary] += sizes[parx];
        }
        else
        {
            parents[pary] = parx;
            sizes[parx] += sizes[pary];
        }
    }
    bool isOneClass(int x, int y) { return Find(x) == Find(y); };

private:
    int *parents, *sizes;
};
class Solution
{
public:
    vector<int> findRedundantConnection(vector<vector<int>> &edges)
    {
        UF uf(edges.size());
        vector<int> ans;
        for (auto edge : edges)
        {
            if (uf.isOneClass(edge[0], edge[1]))
                ans = edge;
            else
                uf.Union(edge[0], edge[1]);
        }
        return ans;
    }
};